package presentation;

import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import solitaire.application.TasDeCartes;
import controle.CCarte;
import controle.CSabot;
import controle.CTasDeCartes;
import controle.CTasDeCartesAlternees;
import controle.ICTas;


public class PSabot extends JPanel implements IPTas{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected CSabot controle;
	//	private IPTas cachee, visible;
	private PCarte carteBaseVisible;
	private int xCachee, xVisible, y;

	/**
	 * Create the panel.
	 */
	public PSabot(CSabot sabot) {
		setLayout(null);
		controle=sabot;
	//	cachee = ((CTasDeCartes)sabot.getCachees()).getPresentation();
	//	visible = ((CTasDeCartes)sabot.getVisibles()).getPresentation();
		
		xCachee = 10;
		xVisible = 90;
		y = 10;
		
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBackground(new Color(47, 79, 79));
		
		PCarte carteCachee = new PCarte();
		add(carteCachee);
		carteCachee.setLocation(xCachee, y);
		carteBaseVisible = new PCarte();
		add(carteBaseVisible);
		carteBaseVisible.setLocation(xVisible, y);
	/*	
		Box horizontalBox = Box.createHorizontalBox();
		horizontalBox.add((PTasDeCartes)cachee);
		horizontalBox.add((PTasDeCartes)visible); 
		add(horizontalBox);
	*/	 
	}

	
	public void depilerCachee() {
		this.remove(getSommetCachee());
	}
	
	@Override
	public void depiler()
	{
		this.remove(getSommet());
		xVisible -= 15;
		repaint();
	}

	@Override
	public void empiler(IPCarte carte) {
		carte.setFaceVisible(true);
		add((PCarte) carte, 0);
		((PCarte) carte).setLocation(xVisible, y);
		xVisible += 15;
		repaint();
	}
	
	public void empilerCachee(IPCarte carte) {
		add((PCarte) carte, 0);
		((PCarte) carte).setLocation(xCachee, y);
	}
	
	@Override
	public PCarte getBase() {
		// TODO Auto-generated method stub
		try {
			return (PCarte) ((CCarte)controle.getBase()).getPresentation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public PCarte getBaseVisible()
	{
		try {
			return (PCarte) ((CCarte) ((CTasDeCartesAlternees)controle.getVisibles()).getBase()).getPresentation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public PCarte getCarte(int n) {
		// TODO Auto-generated method stub
		try {
			return (PCarte) ((CCarte)controle.getCarte(n)).getPresentation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}

	@Override
	public PCarte getSommet() {
		// TODO Auto-generated method stub
		try {
			return (PCarte) ((CCarte)controle.getSommet()).getPresentation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public PCarte getSommetCachee()
	{
		try {
			return (PCarte) ((CCarte) ((CTasDeCartes)controle.getCachees()).getSommet()).getPresentation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void retournerCarte() {
		depilerCachee();
		empiler(getSommetCachee());
	}

	public void setReserve(ICTas tas) {
		for(int i=tas.getNombre(); i>1; i--){
			try {
				IPCarte carte= ((CCarte)((TasDeCartes) tas).getCarte(i)).getPresentation();
				this.empilerCachee(carte);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public int getNombre() {

		return this.controle.getNombre();
	}
	
	public void retourner()
	{
		for(int i = getNombre(); i>0; i--)
		{
			empilerCachee(getCarte(i));
			depiler();
		}
		//xVisible = 90;
	}
}
